package oocl.eshop.ui.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import oocl.eshop.po.Merchant;
import oocl.eshop.service.MerchantManager;
import oocl.eshop.service.impl.MerchantManagerImpl;

/**
 * Servlet implementation class UpdatePasswordServlet
 */
public class UpdatePasswordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	MerchantManager merchantManager = new MerchantManagerImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UpdatePasswordServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		Merchant merchant = (Merchant) session.getAttribute("merchant");
		String oldPassword = request.getParameter("oldPassward");
		System.out.println(oldPassword);
		String newPassword = request.getParameter("newPassword");
		System.out.println(newPassword);
		String newPassword2 = request.getParameter("newPassword2");
		System.out.println(newPassword2);
		System.out.println(merchant.getmPassword());
		if (!oldPassword.equals(merchant.getmPassword())) {
			request.setAttribute("Error", "Old Password Error");
		}
		if (!newPassword.equals(newPassword2)) {
			request.setAttribute("Error", "New Password Error");
		}
		if (request.getAttribute("Error") != null) {
			request.getRequestDispatcher("homePage.jsp").forward(request, response);
		} else {
			merchant.setmPassword(newPassword);
			int i = merchantManager.updateMerchant(merchant);
			if (i != 1) {
				System.out.println("Merchant password update Error");
			}
			session.setAttribute("merchant", merchantManager.findMerchantById(merchant.getmId()));
			request.getRequestDispatcher("homePage.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
